/*
package com.deer.wms.base.system.web.rocketmq;

import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.model.BillInRecord;
import com.deer.wms.base.system.model.bill.BillInDetail;
import com.deer.wms.base.system.model.box.BoxInfo;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.service.BillInRecordService;
import com.deer.wms.base.system.service.bill.IBillInDetailService;
import com.deer.wms.base.system.service.box.BoxInfoService;
import com.deer.wms.base.system.service.box.BoxItemRecordService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.TaskInfoHistoryService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.base.system.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Component
@Slf4j
@Order
public class BillInConsumer extends Thread implements ApplicationRunner {
    @Autowired
    private PullConsumer pullConsumer;
    @Autowired
    private ITaskInfoService taskInfoService;
    @Autowired
    private TaskInfoHistoryService taskInfoHistoryService;
    @Autowired
    private IBoxItemService boxItemService;
    @Autowired
    private IBillInDetailService billInDetailService;
    @Autowired
    private BoxInfoService boxInfoService;
    @Autowired
    private ICellInfoService cellInfoService;
    @Autowired
    private BillInRecordService billInRecordService;
    @Autowired
    private BoxItemRecordService boxItemRecordService;


    public void billInConsumer(){
        pullConsumer.dealMsg();//拉取消息
        MessageExt messageExt = pullConsumer.getMessageExt();//获得消息
        if (messageExt!=null){
        Integer billInId= Integer.parseInt(convertMsgToJsonObj(messageExt).get("billInId").toString());
        Boolean aBoolean = startTask(billInId);
        if (aBoolean){
            //更新下标
            PullConsumer.putMessageQueueOffset(null,PullConsumer.getMessageQueueOffset()+1);
        }
        }
    }



    @Transactional(rollbackFor=Exception.class)
    public Boolean startTask(int billInId) {
        List<TaskInfoDto> taskInfoList = taskInfoService.selectTaskInfoByBillInMasterId(billInId);
        Boolean aBoolean=false;
        for (TaskInfoDto taskInfoDto : taskInfoList) {
            Integer cellId = taskInfoDto.getCellId();//货位ID
            BoxItem boxItem = new BoxItem();
            BoxInfo boxInfo = new BoxInfo();
            BillInRecord billInRecord = new BillInRecord();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = null;
            try {
                time = DateUtil.timeAddByDays(sdf.parse(taskInfoDto.getPdTime()), taskInfoDto.getDateWarning());
            } catch (Exception e) {
                e.printStackTrace();
            }
            String expTime = sdf.format(time);
            billInRecord.setExp(expTime);//计算预计的过期时间
            boxItem.setExp(expTime);//计算预计的过期时间
            //更新明细的入库数量
            BillInDetail billInDetail = new BillInDetail();
            billInDetail.setBillInDetailId(taskInfoDto.getBillInDetailId());//ID
            billInDetail.setQuantityReceived(taskInfoDto.getQuantity());//数量
            billInDetailService.updateBillInInfo(billInDetail);
            //更新库存
            boxItem.setQuantity(taskInfoDto.getQuantity());
            boxItem.setBoxCode(taskInfoDto.getBoxCode());
            boxItem.setBatch(taskInfoDto.getBatch());
            boxItem.setItemCode(taskInfoDto.getItemCode());
            boxItem.setBillInDetailId(taskInfoDto.getBillInDetailId());
            boxItem.setPd(taskInfoDto.getPdTime());
            boxItem.setWareId(1220);
            int re1 = boxItemService.insertBoxItem(boxItem);
            //更新托盘状态
            boxInfo.setBoxCode(taskInfoDto.getBoxCode());
            boxInfo.setBoxState(1);//0-不在货位 1-货位上  2-任务中
            boxInfo.setHasGoods(1);//   0-无货 1-有货
            boxInfo.setBoxCellId(cellId);//货位
            boxInfo.setBoxType(2);//类型：1-料箱  2-托盘
            int re2 = boxInfoService.updateBoxInfo(boxInfo);
            //货位以及状态 cellInfo
            CellInfo cellInfo = new CellInfo();
            cellInfo.setState(1);//状态  ：0-无托盘；1-有货；2-出库中；3-入库中；4故障
            cellInfo.setCellId(cellId);
            int re3 = cellInfoService.updateCellInfo(cellInfo);
            //任务历史表
            int re4 =  taskInfoHistoryService.saveTaskInfoHistory(taskInfoService.selectTaskInfoById(taskInfoDto.getId()));
            // taskInfoService.deleteById(id);//删除已完成的任务表
            //同步入库报表
            billInRecord.setBillInDetailId(taskInfoDto.getBillInDetailId());//出库单id
            billInRecord.setAcceptQuantity(taskInfoDto.getQuantity());//入库数量
            billInRecord.setBoxCode(taskInfoDto.getBoxCode());
            billInRecord.setBarCode(taskInfoDto.getBarCode());
            billInRecord.setPd(taskInfoDto.getPdTime());
            billInRecord.setBatch(taskInfoDto.getBatch());
            int re5 = billInRecordService.insertBillInRecord(billInRecord);
            //同步到库存记录表：
            int re6 = boxItemRecordService.insertBoxItemRecord(boxItem);
            if (re1>0 && re2>0 && re3>0 && re4>0 && re5>0 && re6>0){
                aBoolean = true;
            }else {
                log.error("入库成功后的更新操作失败了,回滚！！！！");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return aBoolean;
            }
        }
        return aBoolean;
    }

    private JSONObject convertMsgToJsonObj(MessageExt messageExt) {
        String taskInfoStr = JSONObject.toJSONString(JSONObject.parse(messageExt.getBody()));
       // String substring = taskInfoStr.substring(taskInfoStr.lastIndexOf("[") + 1, taskInfoStr.lastIndexOf("]"));
        JSONObject obj = (JSONObject) JSONObject.parse(taskInfoStr);
        return obj;
    }

    public void runConsumer() {
        while(true){
            try {
                sleep(20000);
                billInConsumer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Override

    public void run(ApplicationArguments args) throws Exception {
      // runConsumer();
    }
}
*/
